package edu.cqie.smartfarm.controller;


import edu.cqie.smartfarm.constant.CommonConstant;
import edu.cqie.smartfarm.result.R;
import edu.cqie.smartfarm.service.ICityService;
import edu.cqie.smartfarm.vo.CityVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 基础表-省市区 前端控制器
 * </p>
 *
 * @author yyw
 * @since 2024-12-29
 */
@RestController
@RequestMapping("/city")
@RequiredArgsConstructor
@Api(tags = "城市管理")
public class CityController {
    private final ICityService cityService;

 /*   *//**
     * 获取所有省份
     * @return
     *//*
    @ApiOperation(value = "获取所有省份")
    @GetMapping("/provinces")
    @Cacheable(value = CommonConstant.PROJECT_NAME+":cityList", key = "'provinces'")
    public R<List<City>> getProvinces(){
        List<City> provinces = cityService.lambdaQuery().eq(City::getLevel, 1).list();
        List<CityVO> provincesVO = BeanUtil.copyToList(provinces, CityVO.class);
        return R.Success(provincesVO);
    }

    *//**
     * 获取所有城市
     * @return
     *//*
    @ApiOperation(value = "获取所有城市")
    @GetMapping("/getCities/{provinceId}")
    @Cacheable(value = CommonConstant.PROJECT_NAME+":cityList", key = "'cities_'+#provinceId")
    public R<List<City>> getCities(@PathVariable("provinceId") Long provinceId){
        LambdaQueryWrapper<City> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(City::getParentId, provinceId)
                .eq(City::getLevel, 2);
        List<City> cities = cityService.list(queryWrapper);
        List<CityVO> citiesVO = BeanUtil.copyToList(cities, CityVO.class);
        return R.Success(citiesVO);
    }

    @ApiOperation(value = "获取所有区县")
    @GetMapping("/getAreas/{cityId}")
    @Cacheable(value = CommonConstant.PROJECT_NAME+":cityList", key = "'areas_'+#cityId")
    public R<List<City>> getAreas(@PathVariable("cityId") Long cityId){
        LambdaQueryWrapper<City> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(City::getParentId, cityId)
                .eq(City::getLevel, 3);
        List<City> areas = cityService.list(queryWrapper);
        List<CityVO> areasVO = BeanUtil.copyToList(areas, CityVO.class);
        return R.Success(areasVO);
    }*/

    /**
     * 获取所有省市区
     * @return
     */
    @ApiModelProperty(value = "获取所有省市区")
    @GetMapping("/getAllCity")
    @Cacheable(value = CommonConstant.PROJECT_NAME+":cityList", key = "'allCity'")
    public R<List<CityVO>> getAll(){
        List<CityVO> cityVOList = cityService.getAll();
        return R.Success(cityVOList);
    }
}
